/**
 *
 */
package com.honeybees.framework.sso.session.manager;

import java.util.Date;

import com.honeybees.framework.cache.redis.annotation.Cache;
import com.honeybees.framework.sso.SessionConsts;
import com.honeybees.framework.sso.session.MockSession;
import com.honeybees.framework.sso.vo.LoginUser;
import org.springframework.stereotype.Component;

/**
 * <dl>
 * <dt><b> Session管理 </b></dt>
 * <p>
 * <dd>用于单点登录</dd>
 * </dl>
 * <p>
 * Copyright (C) All rights reserved.
 * </p>
 *
 * @author 李远明
 * @version 2015-12-14 17:51:17
 */
@Component
public class MockSessionManager {

    public MockSessionManager() {
    }

    @Cache(namespace = SessionConsts.NS_MOCK_SESSION, method = Cache.Method.Query, keyIndex = 0, seconds = 1800)
    public MockSession getMockSession(String sessionId) {
        return null;
    }

    /**
     * <dl>
     * <dt><b> 更新Session </b></dt>
     * <p>
     * <dd>该方法没有指定key，MethodCacheAspectJ将会从MockSession中获取 @Id 标记的字段的值作为 key</dd>
     * </dl>
     *
     * @param session MockSession
     * @author 李远明
     * @version 2015-12-14 18:01:17
     */
    @Cache(namespace = SessionConsts.NS_MOCK_SESSION, method = Cache.Method.SaveOrUpdateArgument, argIndex = 0, seconds = 1800)
    public void updateSession(MockSession session) {
    }

    /**
     * <dl>
     * <dt><b> 移除Session </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param sessionId SessionID
     * @author 李远明
     * @version 2015-12-14 18:01:33
     */
    @Cache(namespace = SessionConsts.NS_MOCK_SESSION, method = Cache.Method.Delete, keyIndex = 0)
    public void removeSession(String sessionId) {
    }

    /**
     * <dl>
     * <dt><b> 更新指定用户的 SessionID </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param userId    用户ID
     * @param sessionId SessionID
     * @author 李远明
     * @version 2015-12-14 18:04:17
     */
    @Cache(namespace = SessionConsts.NS_MOCK_SESSION_USER, method = Cache.Method.SaveOrUpdateArgument, keyIndex = 0, argIndex = 1, seconds = 1800)
    public void updateSessionIdByUserId(String userId, String sessionId) {
    }

    /**
     * <dl>
     * <dt><b> 获取/更新 指定用户的 SessionID </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param userId 用户ID
     * @return SessionID
     * @author 李远明
     * @version 2015-12-14 18:04:43
     */
    @Cache(namespace = SessionConsts.NS_MOCK_SESSION_USER, method = Cache.Method.Query, keyIndex = 0, seconds = 1800)
    public String getSessionIdByUserId(String userId) {
        return null;
    }

    /**
     * <dl>
     * <dt><b> 移除指定用户的 SessionID </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param userId 用户ID
     * @author 李远明
     * @version 2015-12-14 18:04:43
     */
    @Cache(namespace = SessionConsts.NS_MOCK_SESSION_USER, method = Cache.Method.Delete, keyIndex = 0)
    public void removeSessionIdByUserId(String userId) {
    }

    /**
     * <dl>
     * <dt><b> 创建Session </b></dt>
     * <p>
     * <dd>功能描述</dd>
     * </dl>
     *
     * @param user      用户信息
     * @param sessionId SessionID
     * @param ip        IP地址
     * @return MockSession
     */
    public MockSession createSession(LoginUser user, String sessionId, String ip) {
        String userId = user.getId();

        MockSession session = new MockSession();
        session.setId(sessionId);
        session.setUserId(userId);
        session.setLoginTime(new Date());
        session.setLoginIP(ip);
        session.setRejectMsg(null); // XXX 其实这个不用设值
        session.setUser(user);
        return session;
    }

}
